package com.j180.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.ResultBean;
import com.j180.service.CustomerServiceImpl;
import com.j180.service.ICustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeletServlet",value = "/delete")
public class DeletServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //解决乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获得参数
        String id = req.getParameter("cid");

        ICustomerService iCustomerService = new CustomerServiceImpl();

        int result = iCustomerService.deleteCustomerInfo(Integer.parseInt(id));

        ResultBean resultBean = null;
        if(result==1){
            resultBean = new ResultBean(1,1,"删除成功");
        }else{
            resultBean = new ResultBean(0,0,"删除失败");
        }

        ObjectMapper objectMapper =new ObjectMapper();

        resp.getWriter().print(objectMapper.writeValueAsString(resultBean));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
}
